package com.jblog.user.service;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jblog.user.dao.UserDao;
import com.jblog.user.exception.PasswordFalseExeception;
import com.jblog.user.exception.UserAlreadyExistsException;
import com.jblog.user.exception.UserNotExistsException;
import com.jblog.user.model.User;

@Service
public class UserServiceImpl implements UserService{

	@Autowired
	UserDao userDao;
	
	@Override
	public User findUserByLogin(String loginName, String password) throws UserNotExistsException, PasswordFalseExeception   {
		User user = userDao.selectUserByLogin(loginName);
		if(user == null){
			throw new UserNotExistsException();
		}
		if(!user.getPassword().equals(password)){
			throw new PasswordFalseExeception();
		}
		return user;
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public void regist(User user) throws UserAlreadyExistsException {
		User u = userDao.selectUserByLogin(user.getUsername());
		if(u != null){
			throw new UserAlreadyExistsException();
		}
		userDao.add(user);
	}

}
